package com.example.zhu95.mytimber.adapters;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.MyApplication;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.entity.ArtistBean;
import com.example.zhu95.mytimber.listener.ArtistClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author zhu95
 */
public class ArtistGridAdapter extends RecyclerView.Adapter<ArtistGridAdapter.ViewHolder> {
    private List<ArtistBean> artists;
    private ArtistClickListener artistClickListener;

    public void setArtists(List<ArtistBean> artists) {
        if (this.artists != artists) {
            this.artists.clear();
            this.artists.addAll(artists);
        }
    }

    public ArtistGridAdapter(List<ArtistBean> artists, ArtistClickListener artistClickListener) {
        this.artists = artists;
        this.artistClickListener = artistClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyclerview_item_grid, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final ArtistBean artist = artists.get(position);
        holder.gridItemTv1.setText(artist.getArtist());
        holder.gridItemTv2.setText(artist.getNumOfAlbum() + " 张专辑 | " + artist.getNumOfSong() + " 首歌曲");
        holder.gridItemTv2.setTextColor(MyApplication.getContext().getResources().getColor(R.color.colorGrayText));
        // defect: 2017/12/10 艺术家的图片貌似是没有的，暂时都使用默认值
        String albumArt = artist.getArtist();
        if (!TextUtils.isEmpty(albumArt)) {
            Glide.with(holder.itemView.getContext()).load(albumArt).error(R.drawable.ic_empty_music2).into(holder.gridItemImg);
        } else {
            Glide.with(holder.itemView.getContext()).load(R.drawable.ic_empty_music2).centerCrop().into(holder.gridItemImg);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                artistClickListener.clickArtist(artist.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return artists.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.grid_item_img)
        ImageView gridItemImg;
        @BindView(R.id.grid_item_tv1)
        TextView gridItemTv1;
        @BindView(R.id.grid_item_tv2)
        TextView gridItemTv2;
        @BindView(R.id.grid_item_botttom)
        RelativeLayout gridItemBotttom;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
